package com.wbao.dianniu.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.SpanTextView;
import com.wbao.dianniu.data.KnowledgeData;
import com.wbao.dianniu.listener.IKnowledgeDelListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.KnowDelManager;
import com.wbao.dianniu.manager.KnowGoodManager;
import com.wbao.dianniu.update.KnowledgeHelper;
import com.wbao.dianniu.update.ShareSDKManager;
import com.wbao.dianniu.ui.CreateKnowActivity;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识列表adapter
 * <p>
 * Created by Administrator on 2017/8/30.
 */

public class KnowListAdapter extends BaseAdapter implements IKnowledgeDelListener {

    private final int TPYE_ALL = 1;
    private final int TYPE_MYSELF = 2;
    private final int TYPE_HEAD = 3;
    private LayoutInflater mInflater;
    private List<KnowledgeData> mList;
    private Context mContext;
    private KnowDelManager manager;
    private int deleteId;
    private int type;

    public KnowListAdapter(Context context, int type) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        this.type = type;
        if (null == mList) {
            mList = new ArrayList<KnowledgeData>();
        }
        manager = new KnowDelManager(mContext);
        manager.addKnowledgeDelListener(this);
    }

    public void updateHeadInfo(int accountId, String nickName, String headPic, Integer sex, String label) {
        if (null == mList || mList.size() == 0) {
            return;
        }
        boolean isExist = false;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getUserInfo().accountId == accountId) {
                mList.get(i).getUserInfo().realName = nickName;
                mList.get(i).getUserInfo().headPic = headPic;
                mList.get(i).getUserInfo().sex = sex;
                mList.get(i).getUserInfo().label = label;
                isExist = true;
            }
        }
        if (isExist) {
            notifyDataSetChanged();
        }
    }

    public void updatePartner(int accountId, int partner) {
        if (null == mList || mList.size() == 0) {
            return;
        }
        boolean isExist = false;
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getUserInfo().accountId == accountId) {
                mList.get(i).getUserInfo().partner = partner;
                isExist = true;
            }
        }
        if (isExist) {
            notifyDataSetChanged();
        }

    }


    /**
     * 清空数据
     */
    public void cleanData() {
        if (null != mList) {
            mList.clear();
        }
    }

    public void addData(List<KnowledgeData> list) {
        if (null == list)
            return;
        if (null == mList)
            mList = new ArrayList<>();
        mList.addAll(mList.size(), list);
//        for (int i = 0; i < list.size(); i++) {
//            mList.add(list.get(i));
//        }
    }

    public void addFirstData(KnowledgeData data) {
        if (null != mList) {
            mList.add(0, data);
        }
    }

    public void removeOneData(int knowledgeId) {
        if (null == mList)
            return;
        for (int i = 0; i < mList.size(); i++) {
            if (knowledgeId == mList.get(i).getId()) {
                mList.remove(i);
                break;
            }
        }
    }

    public void goodChanged(int knowledgeId, int isGood, int goodCount) {
        if (null == mList)
            return;
        for (int i = 0; i < mList.size(); i++) {
            if (knowledgeId == mList.get(i).getId()) {
                mList.get(i).setIsGood(isGood);
                mList.get(i).setGoodCount(goodCount);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void replyNotify(int knowledgeId, int answerCounts) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == knowledgeId) {
                mList.get(i).setAnswerCount(answerCounts);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void visitorNotify(int knowledgeId, int visitorCounts) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == knowledgeId) {
                mList.get(i).setVisitorCount(visitorCounts);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void modifyOneData(int knowledgeId, KnowledgeData data) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == knowledgeId) {
                mList.set(i, data);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public void setData(List<KnowledgeData> list) {
        mList = list;
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public List<KnowledgeData> getList() {
        return mList;
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.knowledge_list_item, null);
            holder = new GridHolder();
            holder.headIV = convertView.findViewById(R.id.user_head);
            holder.nameTV = convertView.findViewById(R.id.user_name);
            holder.sexIV = convertView.findViewById(R.id.user_sex);
            holder.partnerIV = convertView.findViewById(R.id.user_partner);
            holder.labelTV = convertView.findViewById(R.id.user_label);
            holder.addV = convertView.findViewById(R.id.add_v_iv);
            holder.headInfoLayout = convertView.findViewById(R.id.user_head_layout);
            holder.titleTV = convertView.findViewById(R.id.know_list_item_title);
            holder.contentTV = convertView.findViewById(R.id.know_list_item_content);
//            holder.visitorTV = (TextView) convertView.findViewById(R.id.include_visitor_tv);
            holder.editTV = convertView.findViewById(R.id.include_edit_tv);
            holder.deleteTV = convertView.findViewById(R.id.include_delete_tv);
            holder.forwardTV = convertView.findViewById(R.id.include_forward_tv);
            holder.praiseTV = convertView.findViewById(R.id.include_praise_tv);
            holder.answerTV = convertView.findViewById(R.id.include_answer_tv);
            holder.auditLayout = convertView.findViewById(R.id.knowledge_audit_layout);
            holder.auditDescTV = convertView.findViewById(R.id.knowledge_audit_desc);
            holder.auditResumeTV = convertView.findViewById(R.id.knowledge_audit_resume);
            convertView.setTag(holder);
        } else {
            holder = (GridHolder) convertView.getTag();
        }
        KnowledgeData data = mList.get(position);

        if (type == TPYE_ALL || type == TYPE_HEAD) {
            holder.auditLayout.setVisibility(View.GONE);
            holder.editTV.setVisibility(View.GONE);
//            holder.visitorTV.setVisibility(View.VISIBLE);
//            holder.line1.setVisibility(View.VISIBLE);
//            holder.line2.setVisibility(View.VISIBLE);
            holder.forwardTV.setVisibility(View.VISIBLE);
            holder.praiseTV.setVisibility(View.VISIBLE);
            holder.answerTV.setVisibility(View.VISIBLE);
        } else if (type == TYPE_MYSELF) {
            holder.auditLayout.setVisibility(View.VISIBLE);
//            holder.visitorTV.setVisibility(View.GONE);

            if (KnowledgeData.STATUS_INIT == data.getStatus()) {
//                holder.line1.setVisibility(View.INVISIBLE);
//                holder.line2.setVisibility(View.INVISIBLE);
                holder.forwardTV.setVisibility(View.INVISIBLE);
                holder.praiseTV.setVisibility(View.INVISIBLE);
                holder.answerTV.setVisibility(View.INVISIBLE);
                holder.editTV.setVisibility(View.INVISIBLE);
            } else if (KnowledgeData.STATUS_REFUSE == data.getStatus()) {
//                holder.line1.setVisibility(View.INVISIBLE);
//                holder.line2.setVisibility(View.INVISIBLE);
                holder.forwardTV.setVisibility(View.INVISIBLE);
                holder.praiseTV.setVisibility(View.INVISIBLE);
                holder.answerTV.setVisibility(View.INVISIBLE);
                holder.editTV.setVisibility(View.VISIBLE);
            } else {
//                holder.line1.setVisibility(View.VISIBLE);
//                holder.line2.setVisibility(View.VISIBLE);
                holder.forwardTV.setVisibility(View.VISIBLE);
                holder.praiseTV.setVisibility(View.VISIBLE);
                holder.answerTV.setVisibility(View.VISIBLE);
                holder.editTV.setVisibility(View.VISIBLE);
            }

        }
        if (null != data) {
            PictureDownloadUtils.displayHeadImage(mContext, holder.headIV, data.getUserInfo().headPic);
            holder.nameTV.setText(data.getUserInfo().realName == null ? "" : data.getUserInfo().realName);
            Utils.showSex(mContext, data.getUserInfo().sex, holder.sexIV);
            Utils.showPartner(mContext, data.getUserInfo().partner, holder.partnerIV);
            holder.labelTV.setText(data.getUserInfo().label == null ? "" : data.getUserInfo().label);
            Utils.showAuth(data.getUserInfo(), holder.addV);
            if (data.getUserInfo().accountId == GlobalContext.getAccountId()) {
                holder.deleteTV.setVisibility(View.VISIBLE);
            } else {
                holder.deleteTV.setVisibility(View.GONE);
            }

            holder.titleTV.setText(data.getSubject() == null ? "" : data.getSubject());
//            Utils.showSpanContent(mContext, data.getShortContent(), data.getNoticeJson(), holder.contentTV);
            holder.contentTV.setEmojiText(data.getShortContent(), data.getNoticeJson());

//            Utils.showVisitorCounts(data.getVisitorCount(), holder.visitorTV);
            Utils.showPriseCounts(data.getAnswerCount(), holder.answerTV);
            Utils.praiseChanged(mContext, data.getIsGood(), data.getGoodCount(), holder.praiseTV);

            if (KnowledgeData.STATUS_REFUSE == data.getStatus()) {
                holder.auditDescTV.setText(mContext.getResources().getString(R.string.know_audit_refuse));
                holder.auditDescTV.setTextColor(mContext.getResources().getColor(R.color.edit_hint_color));
                holder.auditDescTV.setVisibility(View.VISIBLE);
                holder.auditResumeTV.setVisibility(View.VISIBLE);
            } else if (KnowledgeData.STATUS_PASS == data.getStatus()) {
                holder.auditLayout.setVisibility(View.GONE);
            } else {
                holder.auditDescTV.setText(mContext.getResources().getString(R.string.know_audit_init));
                holder.auditDescTV.setTextColor(mContext.getResources().getColor(R.color.red));
                holder.auditDescTV.setVisibility(View.VISIBLE);
                holder.auditResumeTV.setVisibility(View.GONE);
            }

            // 回复 点赞 回答按钮点击事件
            MyClickListener listener = new MyClickListener(data, position, holder.praiseTV);
            holder.forwardTV.setOnClickListener(listener);
            holder.praiseTV.setOnClickListener(listener);
            holder.answerTV.setOnClickListener(listener);
            holder.deleteTV.setOnClickListener(listener);
            holder.auditResumeTV.setOnClickListener(listener);
            holder.editTV.setOnClickListener(listener);
            holder.headInfoLayout.setOnClickListener(listener);
            holder.headIV.setOnClickListener(listener);
            holder.contentTV.setOnClickListener(listener);

        }
        return convertView;
    }

    @Override
    public void onKnowledgeDelFailure(int errorCode, String errorMsg) {
        Notification.toast(mContext, errorMsg);
    }

    @Override
    public void onKnowledgeDelSuccess() {
        // 通知其它界面数据改变
        KnowledgeHelper.getInstance().deleteNotify(deleteId);
    }

    public class GridHolder extends UserInfoHolder {
        public TextView titleTV;
        public SpanTextView contentTV;
        public TextView visitorTV;
        public TextView editTV;
        public TextView deleteTV;
        public TextView forwardTV;
        public TextView praiseTV;
        public TextView answerTV;
        public LinearLayout auditLayout;
        public TextView auditDescTV;
        public TextView auditResumeTV;
    }

    private class MyClickListener implements View.OnClickListener {

        private KnowledgeData data;
        private TextView textview;
        private int position;

        public MyClickListener(KnowledgeData data, int position, TextView textview) {
            this.data = data;
            this.position = position;
            this.textview = textview;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.include_edit_tv:
                    Intent editIntent = new Intent();
                    editIntent.putExtra(Const.INTENT_KNOWLEDGE_ID, data.getId());
                    editIntent.setClass(mContext, CreateKnowActivity.class);
                    mContext.startActivity(editIntent);
                    break;
                case R.id.include_forward_tv:
                    String imageUrl = GlobalContext.cdndownUrl + "logo.png";
                    ShareSDKManager shareSDKManager = ShareSDKManager.getInstance();
                    shareSDKManager.showShare(mContext, data.getShortContent(), 8, imageUrl, data.getId());
                    break;
                case R.id.include_praise_tv:
                    int isGood = data.getIsGood();
                    int goodCount = data.getGoodCount();
                    if (isGood == 0) {
                        isGood = 1;
                        goodCount++;
                    } else {
                        isGood = 0;
                        goodCount--;
                    }
                    mList.get(position).setIsGood(isGood);
                    mList.get(position).setGoodCount(goodCount);
                    goodclick(data.getId(), isGood, goodCount, textview);
                    break;
                case R.id.include_answer_tv:
                    ActivityUtils.intoKnowDetailActivity(mContext, 0, data);
                    break;
                case R.id.include_delete_tv:
                    CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
                    builder.setTitle(mContext.getResources().getString(R.string.delete_know_notice));
                    builder.setPositiveButton(mContext.getResources().getString(R.string.confirm),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    deleteId = data.getId();
                                    manager.knowledgeDel(GlobalContext.getAccountId(), data.getId());
                                }
                            });
                    builder.setNegativeButton(mContext.getResources().getString(R.string.cancel),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    builder.create().show();
                    break;
                case R.id.knowledge_audit_resume:
                    // 显示拒绝原因
                    CustomDialog.Builder build = new CustomDialog.Builder(mContext);
                    build.setTitle(data.getAuditContent());
                    build.setPositiveButton(mContext.getResources().getString(R.string.confirm),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    build.create().show();

                    break;
                case R.id.user_head:
                case R.id.user_head_layout:
                    if (type != TYPE_HEAD) {
                        ActivityUtils.intoHeadInfoActivity(data.getUserInfo().accountId, mContext);
                    }
                    break;
                case R.id.know_list_item_content:
                    ActivityUtils.intoKnowDetailActivity(mContext, 0, data);
                    break;
                default:
                    break;
            }
        }
    }

    private void goodclick(int knowledgeId, int isGood, int goodCount, TextView praiseTV) {
        KnowGoodManager manager = new KnowGoodManager(mContext);
        manager.knowGood(GlobalContext.getAccountId(), knowledgeId);
        // 通知界面点赞数变更  notifyQaChanged(questId,isGood);
        Utils.praiseChanged(mContext, isGood, goodCount, praiseTV);
    }


}
